package com.NeuglsWorkStudio.UI
{
	import com.NeuglsWorkStudio.Colors.RGB;
	import com.NeuglsWorkStudio.UI.Buttons.TTipButton;
	import com.NeuglsWorkStudio.UI.Buttons.TTipButtonEx;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	
	import flashx.textLayout.formats.TextAlign;
	
	/**
	 * This class implements a navigate.
	 * The register point of this class is in the center
	 * @author Neugls
	 * */
	public class Navigate extends Sprite
	{
		
		public static var TextFieldWidth:Number=120;
		public static var ElementMargin:Number=15;
		
		private var _FFirstBtn:TTipButtonEx;
		private var _FLastBtn:TTipButtonEx;
		private var _FNextBtn:TTipButtonEx;
		private var _FPreviousBtn:TTipButtonEx;
		private var _FTextFieldMask:Sprite;
		
		private var _FNavigateTextField:TextField;
		private var _FTextFormat:TextFormat;
		
		private var _FTip:Object;
		private var _FTipBackgroundColor:int;
		
		
		/**
		 * 参数说明：
		 * @param ATip Tip设置。包括以下属性<br/>
		 * <code>
		 * First,Last,Previous,Next,BackgroundColor:String;<br/>
		 * OnFirst,OnLast,OnPrevious,OnNext,OnGoTo,NavigateStr:Function<br/>
		 * FirstImg,FirstMouseOver,FirstMouseDown,FirstDisable<br/>
		 * LastImg,LastMouseOver,LastMouseDown,LastDisable<br/>
		 * PreviousImg,PreviousMouseOver,PreviousMouseDown,PreviousDisable<br/>
		 * NextImg,NextMouseOver,NextMouseDown,NextDisable：Bitmap<br/>
		 * 
		 * </code>
		 * */
		public function Navigate(ATip:Object)
		{
			super();
			_FTip=ATip;
			
			_FTipBackgroundColor=_FTip.BackgroundColor;
			Init();
			
			Paint();
		}
		
		/**提示背景颜色*/
		public function get TipBackgroundColor():int
		{
			return _FTipBackgroundColor;
		}

		/**
		 * @private
		 */
		public function set TipBackgroundColor(value:int):void
		{
			_FTipBackgroundColor = value;
			Paint();
		}

		private function Init():void{
			_FFirstBtn=new TTipButtonEx('',
				(_FTip.FirstImg as DisplayObject).width+15,
				(_FTip.FirstImg as DisplayObject).height+15,
				false);
			_FLastBtn=new TTipButtonEx('',
				(_FTip.LastImg as DisplayObject).width+15,
				(_FTip.LastImg as DisplayObject).height+15,
				false);
			_FNextBtn=new TTipButtonEx('',
				(_FTip.NextImg as DisplayObject).width+15,
				(_FTip.NextImg as DisplayObject).height+15,
				false);
			_FPreviousBtn=new TTipButtonEx('',
				(_FTip.PreviousImg as DisplayObject).width+15,
				(_FTip.PreviousImg as DisplayObject).height+15,
				false);
			
			this.addChild(_FFirstBtn);
			this.addChild(_FLastBtn);
			this.addChild(_FNextBtn);
			this.addChild(_FPreviousBtn);
			
			_FFirstBtn.Tip=_FTip.First;
			_FFirstBtn.TipBackgroundColor=_FTip.BackgroundColor;
			_FFirstBtn.OnClick=_FTip.OnFirst;
			_FFirstBtn.AutoAdjustColor=true;
			
			_FFirstBtn.Image=_FTip.FirstImg;
			if(_FTip.FirstMouseOver){
				_FFirstBtn.MouseOverImg=_FTip.FirstMouseOver;
			}
			if(_FTip.FirstMouseDown){
				_FFirstBtn.MouseDownImg=_FTip.FirstMouseDown;
			}
			if(_FTip.FirstDisable){
				_FFirstBtn.DisabledImg=_FTip.FirstDisable;
			}
			
			//***************************************
			
			_FLastBtn.Tip=_FTip.Last;
			_FLastBtn.TipBackgroundColor=_FTip.BackgroundColor;
			_FLastBtn.OnClick=_FTip.OnLast;
			_FLastBtn.AutoAdjustColor=true;
			
			_FLastBtn.Image=_FTip.LastImg;
			if(_FTip.LastMouseOver){
				_FLastBtn.MouseOverImg=_FTip.LastMouseOver;
			}
			if(_FTip.LastMouseDown){
				_FLastBtn.MouseDownImg=_FTip.LastMouseDown;
			}
			if(_FTip.LastDisable){
				_FLastBtn.DisabledImg=_FTip.LastDisable;
			}
			
			//***************************************
			
			_FNextBtn.Tip=_FTip.Next;
			_FNextBtn.TipBackgroundColor=_FTip.BackgroundColor;
			_FNextBtn.OnClick=_FTip.OnNext;
			_FNextBtn.AutoAdjustColor=true;
			
			_FNextBtn.Image=_FTip.NextImg;
			if(_FTip.NextMouseOver){
				_FNextBtn.MouseOverImg=_FTip.NextMouseOver;
			}
			if(_FTip.NextMouseDown){
				_FNextBtn.MouseDownImg=_FTip.NextMouseDown;
			}
			if(_FTip.NextDisable){
				_FNextBtn.DisabledImg=_FTip.NextDisable;
			}
			
			//***************************************
			_FPreviousBtn.Tip=_FTip.Previous;
			_FPreviousBtn.TipBackgroundColor=_FTip.BackgroundColor;
			_FPreviousBtn.OnClick=_FTip.OnPrevious;
			_FPreviousBtn.AutoAdjustColor=true;
			
			_FPreviousBtn.Image=_FTip.PreviousImg;
			if(_FTip.PreviousMouseOver){
				_FPreviousBtn.MouseOverImg=_FTip.PreviousMouseOver;
			}
			if(_FTip.PreviousMouseDown){
				_FPreviousBtn.MouseDownImg=_FTip.PreviousMouseDown;
			}
			if(_FTip.PreviousDisable){
				_FPreviousBtn.DisabledImg=_FTip.PreviousDisable;
			}
			//***************************************
			
			
			
			
			_FNavigateTextField=new TextField();
			with(_FNavigateTextField)
			{
				multiline = false;
				wordWrap = false;
				background = true;
				type=TextFieldType.INPUT;
				border=true;
				defaultTextFormat = new TextFormat("Tahoma",12,RGB.GetInvertedColor(_FTip.BackgroundColor),null,null,null,null,null,"center");
				width = TextFieldWidth;
				height = 20;
				text='1';
				backgroundColor=RGB.AddToRGBColor(_FTip.BackgroundColor,100);
				restrict = '0-9';
			}
			
			_FTextFieldMask=new Sprite();
			_FTextFieldMask.graphics.beginFill(0xFFFFFF,1);
			_FTextFieldMask.graphics.drawRoundRect(0,0,TextFieldWidth,20,10,10);
			_FTextFieldMask.graphics.endFill();
			this.addChild(_FTextFieldMask);
			
			_FNavigateTextField.mask=_FTextFieldMask;
			
			this.addChild(_FNavigateTextField);
			_FNavigateTextField.addEventListener(MouseEvent.MOUSE_DOWN,clear);
			_FNavigateTextField.addEventListener(KeyboardEvent.KEY_UP,onKeyUp);
		}
		
		private function clear(e:MouseEvent):void
		{
			_FNavigateTextField.text='';
		}
		private function onKeyUp(e:KeyboardEvent):void
		{
			if (e.keyCode == 10||e.keyCode == 13)
			{			
				if(_FNavigateTextField.text=='')
				{
					return;
				}
				if(_FNavigateTextField.text.search('-')>-1)
				{
					return;
				}
				var id:int=int(_FNavigateTextField.text);
				
				if(_FTip.OnGoTo){
					_FTip.OnGoTo(id);
				}
			}
			e.stopPropagation();
		}
		
		/**处理各元件的位置**/
		protected function Paint():void{
			var yy:Number=0;
			var lastX:Number=-TextFieldWidth/2;
			_FNavigateTextField.x=lastX;
			_FNavigateTextField.y=-_FNavigateTextField.height/2;
			
			lastX-=(ElementMargin+_FPreviousBtn.width/2);
			_FPreviousBtn.x=lastX;
			_FPreviousBtn.TipBackgroundColor=_FTipBackgroundColor;
			_FPreviousBtn.BaseColor=_FTipBackgroundColor;
			
			lastX-=(ElementMargin+_FFirstBtn.width);
			_FFirstBtn.x=lastX;
			_FFirstBtn.TipBackgroundColor=_FTipBackgroundColor;
			_FFirstBtn.BaseColor=_FTipBackgroundColor;
			
			lastX=TextFieldWidth/2;
			
			lastX+=(ElementMargin+_FNextBtn.width/2);
			_FNextBtn.x=lastX;
			_FNextBtn.TipBackgroundColor=_FTipBackgroundColor;
			_FNextBtn.BaseColor=_FTipBackgroundColor;
			
			lastX+=(ElementMargin+_FNextBtn.width);
			_FLastBtn.x=lastX;
			_FLastBtn.TipBackgroundColor=_FTipBackgroundColor;
			_FLastBtn.BaseColor=_FTipBackgroundColor;
			
			_FTextFieldMask.x=_FNavigateTextField.x;
			_FTextFieldMask.y=_FNavigateTextField.y;
		}
		
		public function SetNaviageTextFormat(format:TextFormat):void{
			_FTextFormat=format;
			_FTextFormat.align=TextAlign.CENTER; 
			_FNavigateTextField.setTextFormat(format);
		}
		
		public function onPageNumberChanged(Str:String):void{
			_FNavigateTextField.text=Str;
		}
	}
}